package edu.unindra.raport.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.unindra.raport.dao.entity.Siswa;
import edu.unindra.raport.dao.impl.SiswaImpl;

import javax.swing.JComboBox;

import com.toedter.calendar.JDateChooser;

import java.awt.Font;
import java.awt.Color;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class FormDetailMenuSiswaAdd extends JDialog {

	private static final long serialVersionUID = 1L;

	private FormDetailMenuSiswa parent;

	private JPanel jContentPane = null;
	private JButton btnTambah = null;
	private JLabel lblNis;

	private JTextField txtNis;
	private JButton btnReset;
	private JButton btnBatal;
	private JLabel lblTeLahir;
	private JLabel lblKls;
	private JTextField txtTeLahir;

	@SuppressWarnings("unused")
	private String invalidMessage = "";

	private Siswa si = null;
	private SiswaImpl siImpl = null;

	private JLabel lblNamaSiswa;
	private JTextField txtNama;
	private JLabel lblTaLahir;
	@SuppressWarnings("rawtypes")
	private JComboBox cbKls;
	private JLabel lblThnAjaran;
	@SuppressWarnings("rawtypes")
	private JComboBox cbThnAjaran;
	private JDateChooser dateChooser;

	private DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");

	public FormDetailMenuSiswaAdd(FormDetailMenuSiswa parent) {
		this.parent = parent;

		siImpl = new SiswaImpl();

		setModal(true);
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(2);
		setTitle("Tambah Data Siswa");
		setContentPane(getJContentPane());

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 560) / 2, (screenSize.height - 418) / 2,
				560, 281);
	}

	private JPanel getJContentPane() {
		if (this.jContentPane == null) {
			GridBagConstraints gbc_lblNis = new GridBagConstraints();
			gbc_lblNis.gridx = 1;
			gbc_lblNis.anchor = GridBagConstraints.WEST;
			gbc_lblNis.insets = new Insets(2, 2, 5, 5);
			gbc_lblNis.weightx = 0.0D;
			gbc_lblNis.gridy = 0;
			this.lblNis = new JLabel();
			lblNis.setHorizontalAlignment(SwingConstants.LEFT);
			this.lblNis.setText("Nis");
			this.jContentPane = new JPanel();
			GridBagLayout gbl_jContentPane = new GridBagLayout();
			gbl_jContentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
			gbl_jContentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0, 0.0 };
			gbl_jContentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
					0.0, 0.0 };
			this.jContentPane.setLayout(gbl_jContentPane);
			GridBagConstraints gbc_txtNis = new GridBagConstraints();
			gbc_txtNis.gridwidth = 3;
			gbc_txtNis.insets = new Insets(0, 0, 5, 5);
			gbc_txtNis.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNis.gridx = 3;
			gbc_txtNis.gridy = 0;
			jContentPane.add(getTxtCardNumber(), gbc_txtNis);
			this.jContentPane.add(this.lblNis, gbc_lblNis);
			GridBagConstraints gbc_lblNamaSiswa = new GridBagConstraints();
			gbc_lblNamaSiswa.anchor = GridBagConstraints.WEST;
			gbc_lblNamaSiswa.insets = new Insets(0, 0, 5, 5);
			gbc_lblNamaSiswa.gridx = 1;
			gbc_lblNamaSiswa.gridy = 1;
			jContentPane.add(getLblNamaSiswa(), gbc_lblNamaSiswa);
			GridBagConstraints gbc_txtNama = new GridBagConstraints();
			gbc_txtNama.gridwidth = 3;
			gbc_txtNama.insets = new Insets(0, 0, 5, 5);
			gbc_txtNama.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtNama.gridx = 3;
			gbc_txtNama.gridy = 1;
			jContentPane.add(getTxtNama(), gbc_txtNama);
			GridBagConstraints gbc_lblTeLahir = new GridBagConstraints();
			gbc_lblTeLahir.anchor = GridBagConstraints.WEST;
			gbc_lblTeLahir.insets = new Insets(0, 0, 5, 5);
			gbc_lblTeLahir.gridx = 1;
			gbc_lblTeLahir.gridy = 2;
			jContentPane.add(getLblTeLahir(), gbc_lblTeLahir);
			GridBagConstraints gbc_txtTeLahir = new GridBagConstraints();
			gbc_txtTeLahir.gridwidth = 3;
			gbc_txtTeLahir.insets = new Insets(0, 0, 5, 5);
			gbc_txtTeLahir.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtTeLahir.gridx = 3;
			gbc_txtTeLahir.gridy = 2;
			jContentPane.add(getTxtTeLahir(), gbc_txtTeLahir);
			GridBagConstraints gbc_lblTaLahir = new GridBagConstraints();
			gbc_lblTaLahir.insets = new Insets(0, 0, 5, 5);
			gbc_lblTaLahir.gridx = 1;
			gbc_lblTaLahir.gridy = 3;
			jContentPane.add(getLblTaLahir(), gbc_lblTaLahir);
			GridBagConstraints gbc_dateChooser = new GridBagConstraints();
			gbc_dateChooser.gridwidth = 3;
			gbc_dateChooser.insets = new Insets(0, 0, 5, 5);
			gbc_dateChooser.fill = GridBagConstraints.HORIZONTAL;
			gbc_dateChooser.gridx = 3;
			gbc_dateChooser.gridy = 3;
			jContentPane.add(getDateChooser(), gbc_dateChooser);
			GridBagConstraints gbc_lblKls = new GridBagConstraints();
			gbc_lblKls.anchor = GridBagConstraints.WEST;
			gbc_lblKls.insets = new Insets(0, 0, 5, 5);
			gbc_lblKls.gridx = 1;
			gbc_lblKls.gridy = 4;
			jContentPane.add(getLblKls(), gbc_lblKls);
			GridBagConstraints gbc_cbKls = new GridBagConstraints();
			gbc_cbKls.gridwidth = 3;
			gbc_cbKls.insets = new Insets(0, 0, 5, 5);
			gbc_cbKls.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbKls.gridx = 3;
			gbc_cbKls.gridy = 4;
			jContentPane.add(getCbKls(), gbc_cbKls);
			GridBagConstraints gbc_lblThnAjaran = new GridBagConstraints();
			gbc_lblThnAjaran.anchor = GridBagConstraints.WEST;
			gbc_lblThnAjaran.insets = new Insets(0, 0, 5, 5);
			gbc_lblThnAjaran.gridx = 1;
			gbc_lblThnAjaran.gridy = 5;
			jContentPane.add(getLblThnAjaran(), gbc_lblThnAjaran);
			GridBagConstraints gbc_cbThnAjaran = new GridBagConstraints();
			gbc_cbThnAjaran.gridwidth = 3;
			gbc_cbThnAjaran.insets = new Insets(0, 0, 5, 5);
			gbc_cbThnAjaran.fill = GridBagConstraints.HORIZONTAL;
			gbc_cbThnAjaran.gridx = 3;
			gbc_cbThnAjaran.gridy = 5;
			jContentPane.add(getCbThnAjaran(), gbc_cbThnAjaran);
			GridBagConstraints gbc_btnReset = new GridBagConstraints();
			gbc_btnReset.insets = new Insets(0, 0, 0, 5);
			gbc_btnReset.gridx = 4;
			gbc_btnReset.gridy = 6;
			jContentPane.add(getBtnReset(), gbc_btnReset);
			GridBagConstraints gbc_btnBatal = new GridBagConstraints();
			gbc_btnBatal.insets = new Insets(0, 0, 0, 5);
			gbc_btnBatal.gridx = 5;
			gbc_btnBatal.gridy = 6;
			jContentPane.add(getBtnBatal(), gbc_btnBatal);
			GridBagConstraints gbc_btnTambah = new GridBagConstraints();
			gbc_btnTambah.gridx = 6;
			gbc_btnTambah.insets = new Insets(2, 2, 2, 0);
			gbc_btnTambah.anchor = 13;
			gbc_btnTambah.gridy = 6;
			this.jContentPane.add(getBtnTambah(), gbc_btnTambah);
		}
		return this.jContentPane;
	}

	private JButton getBtnTambah() {
		if (this.btnTambah == null) {
			this.btnTambah = new JButton();
			this.btnTambah.setText("Tambah");
			this.btnTambah.setToolTipText("Insert");
			this.btnTambah.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (isDataSiswaValid()) {
						int retVal = 0;
						si = new Siswa();

						si.setNis(txtNis.getText());
						si.setNama(txtNama.getText());
						si.setTeTgl(txtTeLahir.getText());
						si.setTaTgl(dateFormat.format(dateChooser.getDate()));
						si.setKls(Integer.valueOf(cbKls.getSelectedItem()
								.toString()));
						si.setThnAjaran(Integer.valueOf(cbThnAjaran
								.getSelectedItem().toString()));

						try {
							retVal = siImpl.insert(si);
						} catch (SQLException e1) {
							e1.printStackTrace();
						}

						if (retVal > -1) {
							JOptionPane.showMessageDialog(
									FormDetailMenuSiswaAdd.this,
									"Proses penambahan data siswa berhasil");
							parent.getModelTable().addSiswa(si);

							FormDetailMenuSiswaAdd.this.setVisible(false);
						} else {
							JOptionPane.showMessageDialog(
									FormDetailMenuSiswaAdd.this,
									"Proses input data siswa problem");
						}
					} else {
						JOptionPane.showMessageDialog(
								FormDetailMenuSiswaAdd.this,
								"Proses penambahan data siswa  problem");

					}

				}
			});
		}
		return this.btnTambah;
	}

	JTextField getTxtCardNumber() {
		if (txtNis == null) {
			txtNis = new JTextField();
			txtNis.setColumns(10);
		}
		return txtNis;
	}

	private JButton getBtnReset() {
		if (btnReset == null) {
			btnReset = new JButton();
			btnReset.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					buttonResetActionPerformed(e);
				}
			});
			btnReset.setToolTipText("Insert");
			btnReset.setText("Reset");
		}
		return btnReset;
	}

	private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {
		reset();
	}

	private void reset() {
		txtNis.setText("");
		txtNama.setText("");
		txtTeLahir.setText("");
		cbKls.setSelectedIndex(0);
		cbThnAjaran.setSelectedIndex(0);

		setFocusable(true);
	}

	private JButton getBtnBatal() {
		if (btnBatal == null) {
			btnBatal = new JButton();
			btnBatal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					buttonBatalActionPerformed(e);
				}
			});
			btnBatal.setToolTipText("Batal");
			btnBatal.setText("Batal");
		}
		return btnBatal;
	}

	private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {
		FormDetailMenuSiswaAdd.this.setVisible(false);
	}

	private JLabel getLblTeLahir() {
		if (lblTeLahir == null) {
			lblTeLahir = new JLabel();
			lblTeLahir.setText("Tempat Lahir");
		}
		return lblTeLahir;
	}

	private JLabel getLblKls() {
		if (lblKls == null) {
			lblKls = new JLabel();
			lblKls.setText("Kelas");
		}
		return lblKls;
	}

	private JTextField getTxtTeLahir() {
		if (txtTeLahir == null) {
			txtTeLahir = new JTextField();
			txtTeLahir.setColumns(10);
		}
		return txtTeLahir;
	}

	private boolean isDataSiswaValid() {
		if (txtNis.getText().trim().length() == 0) {
			invalidMessage = "Nis Siswa masih kosong";
			return false;
		}

		if (txtNama.getText().trim().length() == 0) {
			invalidMessage = "Nama Siswa masih kosong";
			return false;
		}

		if (txtTeLahir.getText().trim().length() == 0) {
			invalidMessage = "Tempat Lahir  masih kosong";
			return false;
		}

		return true;
	}

	public Siswa getSiswa() {
		return si;
	}

	public void setSiswa(Siswa si) {
		this.si = si;
	}

	private JLabel getLblNamaSiswa() {
		if (lblNamaSiswa == null) {
			lblNamaSiswa = new JLabel();
			lblNamaSiswa.setText("Nama Siswa");
		}
		return lblNamaSiswa;
	}

	private JTextField getTxtNama() {
		if (txtNama == null) {
			txtNama = new JTextField();
			txtNama.setColumns(10);
		}
		return txtNama;
	}

	private JLabel getLblTaLahir() {
		if (lblTaLahir == null) {
			lblTaLahir = new JLabel();
			lblTaLahir.setText("Tanggal Lahir");
		}
		return lblTaLahir;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbKls() {
		if (cbKls == null) {
			cbKls = new JComboBox();
			cbKls.setModel(new DefaultComboBoxModel(new String[] { "1", "2",
					"3", "4", "5", "6" }));
		}
		return cbKls;
	}

	private JLabel getLblThnAjaran() {
		if (lblThnAjaran == null) {
			lblThnAjaran = new JLabel();
			lblThnAjaran.setText("Tahun Ajaran");
		}
		return lblThnAjaran;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getCbThnAjaran() {
		if (cbThnAjaran == null) {
			cbThnAjaran = new JComboBox();
			cbThnAjaran.setModel(new DefaultComboBoxModel(new String[] {
					"2005", "2006", "2007", "2008", "2009", "2010", "2011",
					"2012", "2013", "2014", "2015", "2016", "2017" }));
		}
		return cbThnAjaran;
	}

	private JDateChooser getDateChooser() {
		if (dateChooser == null) {
			dateChooser = new JDateChooser();
			dateChooser.getSpinner().setForeground(Color.BLACK);
			dateChooser.setFont(new Font("Tahoma", Font.BOLD, 11));
		}
		return dateChooser;
	}
}